import {defineStore} from "pinia";
import Page from "@/utils/page";
import {cloneDeep} from "lodash";

export const usePageStore = defineStore('pageStore', {
  state: () => {
    return {
      pageList: [] as Page[],
    }
  },
  actions: {
    getProjectPages(projectId: string) {
      console.log(projectId, this.pageList);
      return this.pageList.filter(e => e.projectId === projectId);
    },
    addProjectPage(obj: any) {
      let page = new Page(obj.projectId, obj.title, obj.url)
      this.pageList.push(page)
    },
    editProjectPage(page: any) {
      this.pageList.forEach(e => {
        if (e.id === page.id) {
          e.title = page.title;
          e.url = page.url;
        }
      })
    },
    deleteProjectPage(id: string) {
      let index = this.pageList.findIndex(e => e.id === id);
      this.pageList.splice(index, 1);
    },
    savePage(id: string, json: any) {
      this.pageList.find(e => e.id === id).json = cloneDeep(json);
    },
    getPageDetail(id: string) {
      return this.pageList.find(e => e.id === id);
    }
  }
})
